package hotel.switchs.oversea.biz.service.impl;

import hotel.base.oversea.constant.CommonConstants;
import hotel.base.oversea.constant.enums.SwitchChannelNameEnums;
import hotel.base.oversea.entity.SwitchPriceBaseSetEntity;
import hotel.base.oversea.service.impl.BaseServiceImpl;
import hotel.base.oversea.utils.JacksonUtil;
import hotel.base.oversea.utils.Result;
import hotel.switchs.oversea.biz.config.service.RedisService;
import hotel.switchs.oversea.biz.mapper.SwitchPriceBaseSetMapper;
import hotel.switchs.oversea.biz.service.SwitchPriceBaseSetService;
import hotel.switchs.oversea.biz.service.SwitchPushRoomService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author lmf
 * @Date 2020/9/10 16:03
 **/
@Service
@Slf4j
public class SwitchPriceBaseSetServiceImpl extends BaseServiceImpl<SwitchPriceBaseSetEntity> implements SwitchPriceBaseSetService {
    @Autowired
    SwitchPriceBaseSetMapper switchPriceBaseSetMapper;
    @Autowired
    SwitchPushRoomService switchPushRoomService;
    @Autowired
    RedisService redisService;
    /**
     * @author: lmf
     * @date: 2020/9/10 16:05
     * 添加价格公式
     */
    @Override
    public Result<Object> addPriceBaseSet(SwitchPriceBaseSetEntity switchPriceBaseSetEntity) {
        switchPriceBaseSetEntity.setFormula(switchPriceBaseSetEntity.getFormula());
        switchPriceBaseSetEntity.setDay(switchPriceBaseSetEntity.getDay());
        switchPriceBaseSetEntity.setCreateTime(new Date());
        int result=switchPriceBaseSetMapper.insert(switchPriceBaseSetEntity);
        String key="switch_online_price_set_by_"+ SwitchChannelNameEnums.CTRIP.getName();
        String qunarKey="switch_online_price_set_by_"+SwitchChannelNameEnums.QUNAR.getName();
        if(result>0){
            redisService.set(key,switchPriceBaseSetEntity);
            redisService.set(qunarKey,switchPriceBaseSetEntity);
            return new Result<>(CommonConstants.SUCCESS,"添加成功");
        }else{
            return new Result<>("添加失败");
        }
    }
    /**
     * @author: lmf
     * @date: 2020/9/14 18:07
     * 开启推送
     */
    public Result<Object> openPush(){
        try {
            Thread.sleep(2000);
            log.info("================酒店上架公式修改开始价格推送============================");
            switchPushRoomService.pushRoomPriceByNewRate();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new Result<>();
    }
    /**
     * @author: lmf
     * @date: 2020/9/10 16:43
     * 获取价格公式列表
     */
    @Override
    public Result<Object> getPriceList() {
        List<SwitchPriceBaseSetEntity> list=switchPriceBaseSetMapper.selectAll();
        return new Result<>(list);
    }
    /**
     * @author: lmf
     * @date: 2020/9/10 17:57
     * 根据id上架价格配置
     */
    @Override
    public Result<Object> deletePriceListById(int id) {
        SwitchPriceBaseSetEntity switchPriceBaseSetEntity=new SwitchPriceBaseSetEntity();
        switchPriceBaseSetEntity.setId(id);
        int result=switchPriceBaseSetMapper.delete(switchPriceBaseSetEntity);
        String key="switch_online_price_set_by_"+ SwitchChannelNameEnums.CTRIP.getName();
        String qunarKey="switch_online_price_set_by_"+SwitchChannelNameEnums.QUNAR.getName();
        if(result>0){
            redisService.remove(key);
            redisService.remove(qunarKey);
            return new Result<>(CommonConstants.SUCCESS,"删除成功");
        }else{
            return new Result<>("删除失败");
        }
    }
    /**
     * @author: lmf
     * @date: 2020/9/10 17:59
     * 根据id上架价格公式
     */
    @Override
    public Result<Object> updatePriceListById(SwitchPriceBaseSetEntity switchPriceBaseSetEntity) {
        log.info("================switchPriceBaseSetEntity的值为{}========", JacksonUtil.objectToJson(switchPriceBaseSetEntity));
        int result=switchPriceBaseSetMapper.updateById(switchPriceBaseSetEntity);
        String key="switch_online_price_set_by_"+ SwitchChannelNameEnums.CTRIP.getName();
        String qunarKey="switch_online_price_set_by_"+SwitchChannelNameEnums.QUNAR.getName();
        if(result>0){
            redisService.set(key,switchPriceBaseSetEntity);
            redisService.set(qunarKey,switchPriceBaseSetEntity);
            return new Result<>(CommonConstants.SUCCESS,"更新成功");
        }else{
            return new Result<>("更新失败");
        }
    }


}
